<?php
function get_tempat_lahir($id){
	$datas = array(
		1 => 'Aceh besar',
		2 => 'Bireun',
		3 => 'Medan',
		4 => 'Lahat',
		5 => 'Palembang',
		6 => 'Padang',
		7 => 'Cilegon',
		8 => 'Tanggerang',
		9 => 'Jakarta',
		10 => 'Bandung',
		
		11 => 'Garut',
		12 => 'Batang',
		13 => 'Madiun',
		14 => 'Surabaya',
		15 => 'Yogyakarta',
		
		16 => 'Denpasar',
		17 => 'Semarang',
		18 => 'Banyuwangi',
		19 => 'Ambarawa',
		20 => 'Tegal',
		
		21 => 'Balikpapan',
		22 => 'Samarinda',
		23 => 'Palangkaraya',
		24 => 'Makasar',
		25 => 'Palu',
	);
	
	return $datas[$id];
}

function get_agama_id($label){
	$query = "SELECT agama_id FROM karyawan_agama WHERE label = '{$label}' ";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['agama_id'];
} 

function generate_email($name){
	$namas = array();
	$namas = explode(" " , $name);
	$emails = array(
		1 => '@gmail.com',
		2 => '@gmail.co.id',
		3 => '@yahoo.com',
		4 => '@pandora.com',
		5 => '#hotmail.com',
		6 => '@msn.com',
		7 => '@telkom.net',
	);
	$ti = rand(1,7);
	$host = $emails[$ti];
	if(strlen( $namas[1]) < 3){
		return $namas[0].$host;
	}
	return strtolower($namas[1]).$host;
}

function parse_karyawan($data){
	$agama_id = get_agama_id($data[5]);
	$tempat_lahir = get_tempat_lahir($data[7]);
	$email = generate_email($data[1]);
	$datas = array(	 
		'nik'	=> my_type_data_str($data[2]),
		'nama'	=> my_type_data_str($data[1]),
		'tmb'	=> my_type_data_str($data[3]),
		'tempat_lahir'	=> my_type_data_str($tempat_lahir),
		'tanggal_lahir'	=> my_type_data_str($data[6]),
		'kelamin'	=> my_type_data_str($data[4]),
		'agama_id'	=> my_type_data_str($agama_id),
		'email'		=> my_type_data_str($email),
		'telepon'	=> my_type_data_str($data[8]),
		'klasifikasi_id' => my_type_data_str(rand(1,9)), 
		'revisi'=> my_type_data_str('1'), 
		'posted_on'=> my_type_data_str('2013-10-20'), 
		'posted_by'=> my_type_data_str('SYSTEM'));
	return my_insert_record('karyawan', $datas);
}

 
function upload_sisa_file_od(){
	$dest ='../files/csv/sf_'.date('Ymd_his').'.csv';
	$ori = $_FILES['filexl']['tmp_name'];
	$upload = move_uploaded_file($ori , $dest);  
	$i=0;
	if($upload){ 
	
		$handle = fopen($dest, "r"); 
		while (($datas = fgetcsv($handle, 1000, ",")) !== FALSE) {  
				parse_karyawan($datas);//parse_kalender_shift($data); 
				$i++;
		}
		fclose($handle);
	}
	return $i ;
}


function upload_form_page(){

 
	$view = form_header( "upload" , "upload"  );
	 
	$file = array(
			'name'=>'filexl',
			'value'=>(isset($_POST['filexl'])? $_POST['filexl'] : ''),
			'id'=>'file' ,'size'=>'70',
			'type'=>'file'
	);
	$form_nominal = form_dynamic($file);
	$view .= form_field_display( $form_nominal , "File CSV" );
	
	$submit = array(
		'value' => ' Proses ',
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function finger_update($datas){
	$mesin_id = $datas[0];
	$nik = $datas[1];
	$karyawan_id = check_karyawan_by_nik($nik);
	$query = "UPDATE a_mesin SET karyawan_id = {$karyawan_id} WHERE finger_id = {$mesin_id} LIMIT 1";
	my_query($query);
}
  